package com.vova.rec.grpc.client;

import com.vova.rec.grpc.common.ConditionsRequest;
import com.vova.rec.grpc.common.DeviceFix;
import com.vova.rec.grpc.common.DeviceFixServiceGrpc;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private DeviceFixServiceGrpc.DeviceFixServiceBlockingStub deviceFixServiceBlockingStub;

    @GetMapping
    public Boolean searchDeviceFix(@RequestParam String id) {
        DeviceFix deviceFix;
//        try {
        deviceFix = deviceFixServiceBlockingStub.searchDeviceFix(ConditionsRequest.newBuilder().setId(id).build());
//        } catch (StatusRuntimeException e) {
//            log.warn("--->> retry grpc call");
//            deviceFix = deviceFixServiceBlockingStub.searchDeviceFix(ConditionsRequest.newBuilder().setId(id).build());
//        }
        log.info("search id={},res={}", id, deviceFix);
        return deviceFix != null;
    }
}
